<?php

namespace app\index\controller;

use think\Db;
use think\Session;
use Wechat\Wechat;

class Accept
{

	public function index()
	{
	    try {
            file_put_contents('./../runtime/wechat.txt', date('Y-m-d H:i:s') . "\n ");
            $weixinconfig = config('wechat');
            file_put_contents('./../runtime/wechat.txt', json_encode($weixinconfig) . "\n ", FILE_APPEND);
            $weixin = new Wechat($weixinconfig);
            file_put_contents('./../runtime/wechat.txt', 1 . "\n ", FILE_APPEND);
            $weixin->valid();
            file_put_contents('./../runtime/wechat.txt', 2 . "\n ", FILE_APPEND);
            $type = $weixin->getRev()->getRevType(); // 消息类型
            file_put_contents('./../runtime/wechat.txt', 3 . "\n ", FILE_APPEND);
            $wxid = $weixin->getRev()->getRevFrom(); // 用户openid
            file_put_contents('./../runtime/wechat.txt', 4 . "\n ", FILE_APPEND);
            $reMsg = "";
            $event = ['event' => ''];

            file_put_contents('./../runtime/wechat.txt', $type . '==' . $wxid . "\n ", FILE_APPEND);
            switch ($type)
            {
                case 'text':
                    $content = $weixin->getRev()->getRevContent();
                    break;
                case 'event':
                    $event = $weixin->getRev()->getRevEvent();
                    $content = json_encode($event);
                    break;
                case 'image':
                    $content = json_encode($weixin->getRev()->getRevPic());
                    $reMsg = "图片很美！";
                    break;
                case 'location':
                    $content = json_encode($weixin->getRev()->getRevGeo());
                    // $reMsg = "您所在的位置很安全！";
                    break;
                default:
                    $content = '';
                    $reMsg = '未知消息类型';
            }
            if ($reMsg)
            {
                echo $weixin->text($reMsg)->reply();
                exit();
            }

            if ($event['event'] == "subscribe")
            { // 用户关注
//                $data = db('scan_log')->where('openid', $wxid)->order('id desc')->find();
//                if ($data) {
//                    $newsData = array([
//                        'Title' => '注册会员',
//                        'Description' => '有推荐人推荐您开启定制生活！！',
//                        'PicUrl' => 'http://' . $_SERVER['HTTP_HOST'] . PUBLIC_URL . 'static/home/images/wechat_cover.jpg',
//                        'Url' => request()->domain() . url('common/team_scan_code', ['parent_openid' => $data['p_openid']])
//                    ]);
//                    echo $weixin->news($newsData)->reply();
//                    exit();
//                }
                Session::clear(); // 清理下缓存
                echo $weixin->text("欢迎关注就行天下 \n专业的酒类服务平台，一站式为客户提供在线购买、正品溯源、约酒交友等服务，重点为商家解决谁来买酒的问题！！")->reply();
                exit();
            } if ($event['event'] == "unsubscribe") { // 取消关注1
                exit();
            }

            // 判断用户是否点击的菜单
            if ($event['event'] == "CLICK") {
                $content = $event['key'];
                switch ($content)
                {
                    case '就行':
                        $newsData = array([
                            'Title' => '就行天下-开发版',
                            'Description' => '专业的酒类服务平台！！',
                            'PicUrl' => request()->domain() . '/static/home/images/jx_beta.png',
                            'Url' => request()->domain() . url('index/index')
                        ]);
                        echo $weixin->news($newsData)->reply(); exit();
                        break;
                    case 'Xydzp':
                        // 获取最新一个状态为启用的大转盘
                        $xydzp = model('xydzp')->order('id desc')->find();
                        $newsData = array([
                            'Title' => '幸运大转盘',
                            'Description' => '转出人生巅峰！！',
                            'PicUrl' => request()->domain() . '/static/images/xydzp.jpg',
                            'Url' => request()->domain() . url('wechat/xydzp/index', ['id' => $xydzp->id])
                        ]);
                        echo $weixin->news($newsData)->reply(); exit();
                        break;
                    case "best":
                    case "new":
                    case "hot":
                        $newsData = array();
                        echo $weixin->news($newsData)->reply();
                        exit();
                        break;
                    case "ddcx":
                        $text = '';
                        echo $weixin->text($text)->reply();
                        exit();
                        break;
                    case "jcbd":

                        break;
                    case "info":
                        echo $weixin->text('')->reply();
                        exit();
                        break;
                    case "qd":
                        break;
                    case 'qdcx':
                        break;
                    default:
                        echo $weixin->text("未定义菜单事件{$content}")->reply();
                        exit();
                        break;
                }
            }
            // $content = $api->getstr($content);
            // 处理用户扫一扫
            if ($event['event'] == "SCAN")
            {
                $content = intval($event['key']); // 场景值ID，临时二维码时为32位非0整型，永久二维码时最大值为100000

                echo $weixin->text('扫码业务处理')->reply();
                exit();
            }
            // 处理用户的输入
            if ($content) {
                if ($content == '1') {
                    echo $weixin->text('你说的有道理啊！！！')->reply();
                    exit();
                }
                if ($content == '就行') {
                    $newsData = array([
                        'Title' => '就行天下-开发版',
                        'Description' => '专业的酒类服务平台！！',
                        'PicUrl' => request()->domain() . '/static/home/images/jx_beta.png',
                        'Url' => request()->domain()
                    ]);
                    echo $weixin->news($newsData)->reply();
                    exit();
                }
                if ($content == 'Xydzp') {
                    // 获取最新一个状态为启用的大转盘
                    $xydzp = model('xydzp')->order('id desc')->find();
                    $newsData = array([
                        'Title' => '幸运大转盘',
                        'Description' => '转出人生巅峰！！',
                        'PicUrl' => request()->domain() . '/static/images/xydzp.jpg',
                        'Url' => request()->domain() . url('wechat/xydzp/index', ['id' => $xydzp->id])
                    ]);
                    echo $weixin->news($newsData)->reply();
                    exit();
                }
                if ($content == '客服') {

                    echo $weixin->text('客服mm来了')->reply();
                    exit();
                }
                if($content == '中奖信息'){
                    $postStr = 	 file_get_contents("php://input");  //代替
                    $postObj = simplexml_load_string ( $postStr, 'SimpleXMLElement', LIBXML_NOCDATA );

                    $openid = $postObj->FromUserName;
                    $info = model('xydzp_prize_record')->where('openid',$openid)->find();
                    $user_name = $info['nickname'];

                    if($user_name){
                        $content = "您好！".$user_name.'，恭喜您获得三等奖一份，请联系客服领取奖励！';
                    }else{
                        $content = "抱歉！未查询到您的中奖信息";
                    }

                    echo $weixin->text($content)->reply();
                    exit;
                }
            }
        } catch (\Exception $e) {
            file_put_contents('./../runtime/wechat.txt', $e->getMessage() . "\n ", FILE_APPEND);
        }
	}
}
